﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WebShop.Classes;
using WebShop.Classes.DataSource;
using WebShop.DataService;
using WebShop.Classes.Messaging;

namespace WebShop.Models
{
    /// <summary>
    /// Base Model class
    /// </summary>
    public abstract class BaseModel
    {
        /// <summary>
        /// Page title
        /// </summary>
        public string Title { get; set; }

        /// <summary>
        /// Languages list
        /// </summary>
        public Dictionary<string, string> Languages { get; private set; }

        /// <summary>
        /// Selected user language
        /// </summary>
        public string Language { get; private set; }

        /// <summary>
        /// Places list
        /// </summary>
        public List<PlaceView> Places { get; private set; }

        /// <summary>
        /// Logged user
        /// </summary>
        public User User { get; set; }

        /// <summary>
        /// Messages list
        /// </summary>
        public List<Message> Messages { get; set; }

        /// <summary>
        /// Full categories tree
        /// </summary>
        public List<CategoryLeaf> CategoryTree { get; private set; }

        public BaseModel() 
        {
            Language = ConfigStorage.Language;
            IDataSource dataSource = DataSourceFactory.Contruct();
            Languages = dataSource.GetLanguages();
            Places = dataSource.GetPlaces();
            Messages = new List<Message>();
            Messages.AddRange(ConfigStorage.Messages);
            ConfigStorage.ClearMessages();

            CategoryTree = GetCategoryTree();
        }

        /// <summary>
        /// Translate 
        /// </summary>
        /// <param name="token">token</param>
        /// <returns>Translate or token if there are no translate</returns>
        public string Translate(string token)
        {
            return Translater.Translate(token);
        }

        /// <summary>
        /// Get tree with all categories
        /// </summary>
        /// <returns>Categories tree</returns>
        private List<CategoryLeaf> GetCategoryTree()
        {
            List<CategoryLeaf> result = new List<CategoryLeaf>();

            IDataSource dataSource = DataSourceFactory.Contruct();
            List<CategoryView> level0 = dataSource.GetCategories();

            foreach (CategoryView item in level0)
            {
                CategoryLeaf leaf = new CategoryLeaf();
                leaf.Item = item;
                leaf.Sub = dataSource.GetCategories(item.Id);

                result.Add(leaf);
            }

            return result;
        }

        public string HtmlDecode(string value)
        {
            string result = value;

            result = result.Replace("&lt;", "<");
            result = result.Replace("&gt;", ">");

            return result;
        }  
    }
}